var app = angular.module('app', ['ngRoute', 'ngCookies','ngAnimate', 'route-segment', 'view-segment', 'mainControllersModule'
								, 'authenticationModule', 'Security', 'loginModule', 'rootModule', 'activityModule']);

var wsUrl = 'http://200.68.65.249:58080/taller-webapp/api/'; // Usar este valor para probar con wev services en la VM
var mockUrl = 'data/'; // Usar este valor para prueba con mocks
	
var urlBase = mockUrl;

app.config(function($routeSegmentProvider, $routeProvider) {
    
    // Configuring provider options
    
    $routeSegmentProvider.options.autoLoadTemplates = true;
    
    // Setting routes. This consists of two parts:
    // 1. `when` is similar to vanilla $route `when` but takes segment name instead of params hash
    // 2. traversing through segment tree to set it up
  
    $routeSegmentProvider
    
    		.when('/login',				'login')
    		.when('/home/:id/actividad',	'main.home')
    		.when('/home/prefs',    'main.prefs')
    		.when('/home/archivos',	'main.archivos')
			.when('/home/foro',		'main.foro')
			.when('/home/enlaces',		'main.enlaces')
			.when('/home/crearActividad',		'main.crearActividad')
			.when('/home/eliminarActividad',		'main.eliminarActividad')
			.when('/home/modificarActividad',		'main.modificarActividad')
			.when('/home/listarActividad',		'main.listarActividad')
			.when('/home/tabla',		'main.tabla')    		
    		.when('/home/:id',      'main.itemInfo.tab1')
    		.when('/home/:id/X',    'main.itemInfo.tab1')
    		.when('/home/:id/Y',    'main.itemInfo.tab2')
			.when('/home',				'main.root')
			
			.when('/home/actividad/:idActivity',	'main.activity')			
			
    		.when('/section2',          's2')
        .when('/section2/:id',      's2.itemInfo')
        
        .when('/section3',          's3')
            
		.segment('/', {
			templateUrl: 'templates/home.html',
            controller: MainCtrl})
        
        .segment('main', {
            templateUrl: 'templates/home.html',
            controller: MainCtrl})
            
        .within()

			.segment('root', {
				templateUrl:	'templates/root.html',
				controller: 'rootController'})            
            
            .segment('home', {
                templateUrl: 'templates/frames/actividad.html',
                controller: 'activityLoader',
                dependencies: ['id']})
                
            .within() 
                
                .segment('tab1', {
                    templateUrl: 'templates/frames/tabs/tab1.html'})
                    
                .segment('tab2', {
                    templateUrl: 'templates/frames/tabs/tab2.html'})
                
            .up()
            .segment('actividad', {
            		templateUrl: 'templates/frames/actividad.html',
            		controller: 'activityLoader'})
            .segment('prefs', {
                templateUrl: 'templates/frames/prefs.html',
                controller: 'loginController'})
            .segment('archivos', {
            		templateUrl: 'templates/frames/archivos.html'}) 
            .segment('tabla', {
            		templateUrl: 'templates/frames/tabla.html'}) 
            .segment('foro', {
            		templateUrl: 'templates/frames/foro.html'})
            .segment('enlaces', {
            		templateUrl: 'templates/frames/enlaces.html'})            		
			.segment('crearActividad', {
					templateUrl: 'templates/frames/crearActividad.html',
					controller: 'activityLoader'})
			.segment('eliminarActividad', {
					templateUrl: 'templates/frames/eliminarActividad.html',
					controller: 'activityLoader'})
			.segment('modificarActividad', {
					templateUrl: 'templates/frames/modificarActividad.html',
					controller: 'activityLoader'})
			.segment('listarActividad', {
					templateUrl: 'templates/frames/listarActividad.html',
					controller: 'activityLoader'})
        .up()
        
        .segment('s2', {
            templateUrl: 'templates/section2.html',
            controller: MainCtrl})
            
        .within()
            
        .up()
            
        .segment('s3', {
            templateUrl: 'templates/section3.html'})
            
        .segment('login', {
          	templateUrl:	'templates/login.html',   
				controller: 'loginController'});
			
	// Also, we can add new item in a deep separately. This is useful when working with
    // routes in every module individually
            
    $routeSegmentProvider
    
        .when('/home/:id/Z',    'main.itemInfo.tab3')  
        
        .within('main')
            .within('itemInfo')
                .segment('tab3', {
                    templateUrl: 'templates/frames/tabs/tab3.html'});
                    
                    
    // This is some usage of `resolve`, `untilResolved` and `resolveFailed` features
            
    $routeProvider.when('/index.html', { redirectTo: '/home' });
	//$routeProvider.otherwise({redirectTo: '/login'}); 
}) ;

app.value('loader', {show: false});



function MainCtrl($scope, $routeSegment, loader, $Security) {
    $scope.$routeSegment = $routeSegment;
    $scope.urlServices = urlBase;
    $scope.isCoordinator = $Security.isCoordinator();
    $scope.isParticipant = $Security.isParticipant();    
    $scope.loader = loader;
	$scope.admin = true;
	$scope.lower = "pincho";    
	$scope.usr = 'admin';
	$scope.rol = 'Coordinador';
    $scope.$on('routeSegmentChange', function() {
        loader.show = false;
        $scope.test = { btnClicked: false };
        $scope.items = [ 1 ];    
    });
}

function Section1ItemCtrl($scope, $routeSegment) {

    $scope.$routeSegment = $routeSegment;
    $scope.item = { id: $routeSegment.$routeParams.id };
    $scope.test = { textValue: '' };
}

function Section2Ctrl($scope, $routeSegment) {

    $scope.$routeSegment = $routeSegment;
    $scope.test = { textValue: '' };
    $scope.items = [ 1,2,3,4,5,6,7,8,9 ];
}

function ErrorCtrl($scope, error) {
    $scope.error = error;
}

function SlowDataCtrl($scope, data, loader) {
    loader.show = false;
    $scope.data = data;
}


